﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Celltest
{
    public class Entity
    {
        public byte B;
        public Position Pos;
         public Entity(byte b, Position pos)
         {
             this.B = b;
             this.Pos = pos;
         }
    }

    public class CreateArrayCell
    {
        private bool done = false;
        LoadSave ls = new LoadSave();
        private int height;
        private int width;
        public List<Entity> ent = new List<Entity>(); 

        /// <summary>
        /// This will load from a file and recreate it so that the binary reader can save it, this is a way to make it easy for a human to create
        /// a cell as binary dont tell us much. this is how it work: 1 = top left conner, 2 = bottom left conner, 3 = top right conner, 4 = bottom right conner
        /// t = T section, s = upside down t section, W= wall facing up, w = wall facing flat, d = door, 
        public CreateArrayCell(string worldfile, int width, List<string> temp )
        {
            int x = 0;
            int y = 0;
            ent.Clear();
            byte b = 176;
            Position pos = new Position();
            for (int i = 0; i < temp.Count; i++)
            {
                if (x == width)
                {
                    x = 0;
                    y++;
                }
                var s = temp[i];
                switch (s)
                {
                    case "1":
                        b = 201;
                        break;
                    case "2":
                        b = 200;
                        break;
                    case "3":
                        b = 183;
                        break;
                    case "4":
                        b = 188;
                        break;
                    case "s":
                        b = 208;
                        break;
                    case "t":
                        b = 203;
                        break;
                    case "d":
                        b = 68;
                        break;
                    case "W":
                        b = 186;
                        break;
                    case "w":
                        b = 205;
                        break;
                    case " ":
                        b = 0;
                        break;
                    case "c":
                        b = 0;
                        break;
                }
                x++;
                
                ent.Add(new Entity(b,new Position(x,y)));
                if (i == temp.Count-1)
                    done = true;
            }
            if (done)
            {
                save(worldfile);
            }
        }
        private void save(string worldfile)
        {
            ls._Binarywriter(worldfile, ent);
            done = false;
        }
    }

    class ArrayCell
    {
       

        public ArrayCell(int width, int height)
        {
            
        }
    }
    class Cell
    {
        byte b = 0;
        public Position pos = new Position();
        GUI gui = new GUI();
        public Cell(int startx= 0, int starty = 0)
        {
        }
        private void Top(int width)
        {
            pos = new Position();
            pos.Y++;
            
            for (int i = 0; i < width; i++)
            {
                if (pos.X == 0)
                {
                    b = 201;
                }
                else if(pos.X == width-1)
                {
                    b = 183;
                }
                else
                {
                    b = 205;
                }
                pos.X++;
                gui.WriteAt(b, pos);
            }
            pos = new Position();

        }
        private void LeftWall(int height)
        {
            pos.Y++;
            pos.X++;   
            for (int i = 0; i < height; i++)
            {
                b = 186;
                pos.Y++;
                gui.WriteAt(b, pos);
            }
            pos= new Position();
         }

        private void RigtWall(int height, int width)
        {
            pos.Y++;
            
            for (int i = 0; i < height; i++)
            {
                b = 186;
                pos.Y++;
                pos.X = width;
                gui.WriteAt(b, pos);
            }
            pos = new Position();
        }
        
        private void MakeBottom(int height, int width)
        {
            height++;
            pos.Y++;
            pos.Y = height;
            for (int i = 0; i < width; i++)
            {
                if(pos.X == 0)
                {
                    b = 212;
                }
                else if(pos.X == width-1)
                {
                    b = 190;
                }
                else
                {
                    b = 205;
                }
                pos.X++;

                gui.WriteAt(b,pos);
            }
        }


        public void MakeCell( int width, int height)
        {
            Top(width);
            LeftWall(height);
            RigtWall(height,width);
            MakeBottom(height,width);

        }
    }
}
